package lib.gt.map;

import java.util.ArrayList;
import java.util.List;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;

import android.graphics.drawable.Drawable;

public class MapItemizedOverlay extends AbsItemizedOverlay {
	
	private final List<MapOverlayItem> mOverlayItems = new ArrayList<MapOverlayItem>();
	
	private boolean isTapOverlay;

	public MapItemizedOverlay(Drawable defaultMarker) {
		super(boundCenter(defaultMarker));
//		super(defaultMarker);
	}
	
	public Drawable boundDrawableCenter(Drawable marker) {
		return super.boundCenter(marker);
	}

	@Override
	protected MapOverlayItem createItem(int i) {
		if(i >= 0 && i < size()) {
			return mOverlayItems.get(i);
		}
		return null;
	}

	@Override
	public int size() {
		return mOverlayItems.size();
	}
	
	public void addOverlayItem(MapOverlayItem item) {
		mOverlayItems.add(item);
		populate();
	}
	
	@Override
	protected boolean onTap(int index) {
		boolean interrupt = super.onTap(index);
		if(!interrupt && OverlayManager.hasInit()) {
			isTapOverlay = true;
			OverlayManager.getInstance().getMapView().getController().animateTo(mOverlayItems.get(index).getPoint());
			OverlayManager.getInstance().showPopupView(index);
		}
		return interrupt;
	}

	@Override
	public boolean onTap(GeoPoint point, MapView mapView) {
		boolean interrupt = super.onTap(point, mapView);
		if(!interrupt && OverlayManager.hasInit()) {
			if(!isTapOverlay) {
				OverlayManager.getInstance().hidePopupViewForSingleMode();
			}
			else {
				isTapOverlay = false;
			}
		}
		return interrupt;
	}
	
	public void clear() {
		for(MapOverlayItem item : mOverlayItems) {
			item.release();
		}
		mOverlayItems.clear();
	}

	@Override
	public void release() {
		super.release();
		clear();
	}
}
